/**
 * Created by beck on 2015/8/20.
 */
/**
 * Created by beck on 2015/8/19.
 */
$(document).ready(function(){
    var $token = window.localStorage.getItem("token");
    init();
    getCommentList($token);
    commentBack($token);
    $("#zan").click(function(){
        var $tag = $(this).attr("tag");
        clickZan($tag,$token);
    });
    $("#publish").click(function(){
        publishComment();
    });
    $("#enter").click(function() {
		$("#dialogbox").css("display","block");
	});
    $("#calloff").click(function() {
		$("#dialogbox").css("display","none");
	});
    $("#sure").click(function(){
    	var type = $("#enter").attr("tag");
    	enterTeam(type);
    });
});
//初始化
function init(){
    var token = window.localStorage.getItem("token");
    var $mid = window.localStorage.getItem("mid");
    var $groupid= window.localStorage.getItem("groupId");
    var Post ="mid="+$mid+"&groupid="+$groupid+"&infoid="+getUrlParam("infoid")+"&token="+token+"&did=33333";
    $.ajax({
        url:'../../api/group/getgroupinforminfoObject',
        type:'post',
        data:Post,
        success:function(msg){
            if(msg.code==100){
                $("h4").html(msg.datum.n2);
                $("#img").attr("src",msg.datum.n1);
                $("#time").html(msg.datum.n4);
                $("#content").html(msg.datum.n3);
                $("#comcount").html(msg.datum.n5);
                $("#zancount").html(msg.datum.n6);
                if(msg.datum.n7 ==1){
                    $("#zan").attr({src:"../source/images/Teal_heart.png",tag:"0"});
                }else{
                    $("#zan").attr({src:"../source/images/Red_heart.png",tag:"1"});
                }
                if (msg.datum.n8 ==0){
					$("#enter").css("background-color","rgba(0,0,0,0.5)").attr("tag","1");
				}else{
					$("#enter").css("background-color","rgba(255,255,255,1)").attr("tag","0");
				}
            }
        }
    });
}
//参加
function enterTeam(type){
	var $infoid = getUrlParam("infoid");
    var Post = "mid="+basedata.mid+"&groupid="+basedata.groupid+"&did="+commondata.groupinform+"&token="+basedata.token+"&infoid="+$infoid+"&type="+type;
    $.ajax({
    	url:'../api/group/joingroupinforminfo',
    	type:'post',
    	data:Post,
    	success:function(msg){
    		if(msg.code == 100){
    			$("#backinfo").text(msg.message);
    			$("#dialogbox").css("display","none");
    		}
    	}
    });
}
//点赞
function clickZan(type,token){
    var $mid = window.localStorage.getItem("mid");
    var $groupid = window.localStorage.getItem("groupId");
    var $infoid = getUrlParam("infoid");
    var Post = "mid="+$mid+"&groupid="+$groupid+"&pid="+$infoid+"&type="+type+"&token="+token+"&did=33333";
    $.ajax({
        url:'../api/userproetformid',
        type:'post',
        data:Post,
        success:function(msg){
            if(msg.code ==100){
                if(type ==0){
                    $("#zan").attr({src:"../source/images/Red_heart.png",tag:"1"});
                    $("#zancount").html(msg.message);
                }else{
                    $("#zan").attr({src:"../source/images/Teal_heart.png",tag:"0"});
                    $("#zancount").html(msg.message);
                }
            }
        }
    });
}
//获取回复列表
function getCommentList(token){
    var $infoid = getUrlParam("infoid");
    var Post ="pid="+$infoid+"&did=33333&pagesize=2&token="+token;
    $.ajax({
        url:'../api/getreplyreviewbymidforlist',
        type:'post',
        data:Post,
        success:function(){
        }
    });
}
//发表回复
function publishComment(){
    var $mid = window.localStorage.getItem("mid");
    var $groupid = window.localStorage.getItem("groupId");
    var $token = window.localStorage.getItem("token");
    var $infoid = getUrlParam("infoid");
    var $comment = $("#commentcontent").val();
    var Post = "mid="+$mid+"&groupid="+$groupid+"&pid="+$infoid+"&did=33333&conment="+$comment+"&token="+$token;
    $.ajax({
        url:'../api/sendreviewbymid',
        type:'post',
        data:Post,
        success:function(msg){
            if(msg.code ==100){
                var $p = '<p><span>'+$mid+':</span><span>'+$comment+'</span></p>';
                $("#comment-list").append($p);
                $(".dig").css("display","none");
            }
        }
    });
}
//获取业务回复评论列表
function commentBack($token){
    var $infoid=getUrlParam("infoid");
    var Post ="pid="+$infoid+"&did=33333&pagesize=5&token="+$token;
    $.ajax({
        url:'../api/getbussdatareviewbypidforlist',
        type:'post',
        data:Post,
        success:function(){

        }
    });
}
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}